package com.mcxx.modules.common.service.impl;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.common.service.StatisticService;
import com.mcxx.modules.base.service.read.AreaReadService;
import com.mcxx.modules.statistics.dto.MinLivingDetailDTO;
import com.mcxx.modules.statistics.dto.MiniLivingAuditSummaryDTO;
import com.mcxx.modules.statistics.dto.StatisticDTO;
import com.mcxx.modules.statistics.param.StatisticParam;
import com.mcxx.modules.statistics.service.read.StatisticReadService;
import com.mcxx.modules.system.entity.AreaSalvationEntity;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.component.org.orguser.area.entity.TaAreaPo;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@Service
@TaTransactional
public class StatSingleSummaryServiceImpl extends StatisticService {
    @Autowired
    private StatisticReadService statisticReadService;

    @Autowired
    private AreaReadService areaReadService;

    @Override
    public List<? extends StatisticDTO> getStatistic(StatisticParam param, UserAccountVo accountVo, String standType) {
        List<TaAreaPo> taAreaPos = new ArrayList<TaAreaPo>();
        List<Integer> levels = new ArrayList<Integer>();
        String name = super.commonStatisticStart(param, accountVo, taAreaPos, levels);
        if (taAreaPos.isEmpty()) {
            throw new BizException(FailureCode.ERR_100002);
        }

        TaAreaPo curTaAreaPo = taAreaPos.get(0);

        String validCode = areaUtil.getValidCode(curTaAreaPo.getAreaCode(), curTaAreaPo.getAreaLevel());

        List<TaAreaPo> childArea = areaReadService.getChildrenByLevel(validCode, levels);
        List<TaAreaPo> validCounty = childArea.stream().filter(e -> !e.getAreaCode().endsWith("99900")
                && !e.getAreaName().startsWith("测试") && !e.getAreaName().endsWith("省本级")).collect(Collectors.toList());


        String formatDate = param.getMonth();
        formatDate = formatDate.substring(0, 4) + "-" + formatDate.substring(4) + "-01";

        List<MiniLivingAuditSummaryDTO> results = new ArrayList<>();

        List<MinLivingDetailDTO> detailResults = null;
        if (standType.equals(Constant.BusinessStandType.CITY_DIBAO)) {
            name += "城市低保审批汇总表";
            detailResults = statisticReadService.getMiniLivingAuditSummary(validCode,formatDate, Constant.BusinessStandType.CITY_DIBAO);
        } else {
            name += "农村低保审批汇总报表";
            detailResults = statisticReadService.getMiniLivingAuditSummary(validCode, formatDate, Constant.BusinessStandType.COUNTY_DIBAO);
        }

        for (TaAreaPo taArea : validCounty) {
            String taAreaValidCode = areaUtil.getValidCode(taArea.getAreaCode(), taArea.getAreaLevel());
            List<MinLivingDetailDTO> list = detailResults.stream().filter(temp -> temp.getAreaCode().startsWith(taAreaValidCode)).collect(Collectors.toList());

            MiniLivingAuditSummaryDTO dto = new MiniLivingAuditSummaryDTO();
            dto.setAreaCode(taArea.getAreaCode());
            dto.setArea(taArea.getAreaName());
            dto.setTitle(name);

            MiniLivingAuditSummaryDTO.dealWithAllAndSingleSave(list,dto);

            Map<String, List<MinLivingDetailDTO>> saveTypeMap = list.stream().collect(Collectors.groupingBy(MinLivingDetailDTO::getSaveType));

            List<MinLivingDetailDTO> allSaveList = saveTypeMap.get("01");
            MiniLivingAuditSummaryDTO.dealWithAllSave(allSaveList,dto);

            List<MinLivingDetailDTO> singleSaveList = saveTypeMap.get("02");
            MiniLivingAuditSummaryDTO.dealWithSingleSave(singleSaveList,dto);
            results.add(dto);
        }
        super.commonStatisticEnd(name, null, taAreaPos, levels, results);
        return results;
    }

    @Override
    public void fillSalvation(StatisticDTO dto, List<String> standards, List<AreaSalvationEntity> salivations) {

    }

}
